//
//  TTEmotionTools.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/30.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

// 最大行数
let SHEmoticonMaxRow = 3
// 最大列数
let SHEmoticonMaxCol = 7
// 最多表情数
let SHEmoticonMaxCount = SHEmoticonMaxRow*SHEmoticonMaxCol - 1

class TTEmotionTools: NSObject {
    
//  单例
    static let sharedEmotionTools:TTEmotionTools = TTEmotionTools()
   
    
    lazy var emotionBundle : NSBundle = {
        
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
       
        return NSBundle(path: path)!
    }()
//  获取各个模型数组
    lazy var defaultEmotion:[TTEmoticonsModel] = {
        
        return self.getEmotion("default/")
    }()
    
    lazy var emojiEmotion:[TTEmoticonsModel] = {
       
        return self.getEmotion("emoji/")
    }()
    
    lazy var lxhEmotion:[TTEmoticonsModel] = {
       
        return self.getEmotion("lxh/")
    }()
 
//   获取三维数组
    lazy var emotionSection:[[[TTEmoticonsModel]]] = {
       
        return [
            self.getEmotionRow(self.defaultEmotion),
            self.getEmotionRow(self.emojiEmotion),
            self.getEmotionRow(self.lxhEmotion)
        ]
    }()
    
//  截取模型数组 获取二维数组
    
    func getEmotionRow(emotion:[TTEmoticonsModel])->[[TTEmoticonsModel]]{
        
        let rowCount = (emotion.count + SHEmoticonMaxCount - 1) / SHEmoticonMaxCount
        
        var emotionItem:[[TTEmoticonsModel]] = [[TTEmoticonsModel]]()
        
        for i in 0..<rowCount{
            
            let location = i * SHEmoticonMaxCount
            var length = SHEmoticonMaxCount
            
            if location + length > emotion.count{
                
                length = emotion.count - location
            }
            
            let range = NSMakeRange(location, length)
            
            let emotionRow:[TTEmoticonsModel] = (emotion as NSArray).subarrayWithRange(range) as! [TTEmoticonsModel]
            
            emotionItem.append(emotionRow)
        }
        
        return emotionItem
    }
    

//   模型
    func getEmotion(name:String)->[TTEmoticonsModel]{
        
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)
        
        let filePath = NSBundle(path: path!)?.pathForResource("\(name)/info.plist", ofType: nil)
        
        let tempArray = NSArray(contentsOfFile: filePath!)
        
        var emotionArray:[TTEmoticonsModel] = [TTEmoticonsModel]()
        
        for value in tempArray!{
            
            let model = TTEmoticonsModel(dict: value as! [String : AnyObject])
            
            model.path = "\(name)\(model.png ?? "")"
            
            emotionArray.append(model)
        }
        
        return emotionArray
    }
    
    func getEmotoinModel(chs:String)->TTEmoticonsModel?{
        
        for value in defaultEmotion{
            
            if chs == value.chs{
                
                return value
            }
            
        }
        
        
        for value in lxhEmotion{
            
            if chs == value.chs{
                
                return value
            }
        }
        
        return nil
    }
}